import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:wu_you/res/colors.dart';

extension PaddingExt on Widget {
  Widget addPadding(EdgeInsetsGeometry padding) {
    return Padding(padding: padding, child: this);
  }
  Widget addGesture(GestureTapCallback onTap){
    return GestureDetector(onTap: onTap,child: this,);
  }
  Container addGradient({Color startColor = AppColors.primaryColor,Color endColor = AppColors.secondColor,EdgeInsetsGeometry padding,Color color,BorderRadiusGeometry borderRadius,Size size,ShapeBorder shape}){
    if(shape != null){
      if(size != null){
        return Container(width:size.width,height: size.height,child: this,color: color,padding: padding,decoration: ShapeDecoration(gradient: LinearGradient(colors: [startColor,endColor]),shape: shape),);
      }
      return Container(child: this,color: color,padding: padding,decoration: ShapeDecoration(gradient: LinearGradient(colors: [startColor,endColor]),shape: shape),);
    }
    if(size != null){
      return Container(width:size.width,height: size.height,child: this,color: color,padding: padding,decoration: BoxDecoration(gradient: LinearGradient(colors: [startColor,endColor]),borderRadius: borderRadius),);
    }
    return Container(child: this,color: color,padding: padding,decoration: BoxDecoration(gradient: LinearGradient(colors: [startColor,endColor]),borderRadius: borderRadius),);
  }
  Widget addRadius(double radius,{BorderRadius borderRadius}){
   return ClipRRect(
      borderRadius:borderRadius != null ? borderRadius: BorderRadius.circular(radius),
      child: this);
  }
  Widget addRichText(String oneStr,String twoStr,{double space,Color oneColor,Color twoColor,bool firstBold = false,double fontSize}){
    return RichText(text: TextSpan(children: [
      TextSpan(text: oneStr,style: TextStyle(color: oneColor ?? AppColors.textColor,fontWeight: firstBold?FontWeight.bold:null,fontSize: fontSize)),
      TextSpan(text: twoStr,style: TextStyle(color: twoColor ?? AppColors.primaryColor,fontSize: fontSize))
    ]),);
  }
  Widget addExpand(){
    return Expanded(child: this,);
  }
  Widget addGradientAppbar(){
    return PreferredSize(preferredSize: Size.fromHeight(56),child: this.addGradient());
  }
  Widget addVisibility(bool visible){
    return Visibility(visible: visible,child: this);
  }
  Widget addContainer(
      {Color color = Colors.white,
        double width,
        double height,
        EdgeInsetsGeometry padding,
        AlignmentGeometry alignment,
        EdgeInsetsGeometry margin,
        double radius = 5,
        isShadow = false}) {
    if (radius != null) {
      return Container(
        child: this,
        alignment: alignment,
        padding: padding,
        margin: margin,
        width: width,
        height: height,
        decoration: BoxDecoration(
            color: color,
            borderRadius: BorderRadius.circular(radius),
            boxShadow: isShadow
                ? [
              BoxShadow(
                  color:
                  AppColors.authUnselectedLabelColor.withOpacity(0.2),
                  offset: Offset(0, 0.5), //阴影xy轴偏移量
                  blurRadius: 5.0, //阴影模糊程度
                  spreadRadius: 3 //阴影扩散程度
              )
            ]
                : null),
      );
    } else {
      return Container(
        child: this,
        padding: padding,
        margin: margin,
        width: width,
        height: height,
        color: color,
      );
    }
  }
  Widget addAspectRatio(double aspectRatio){
    return AspectRatio(aspectRatio: aspectRatio,child: this,);
  }
  Widget addSingleScrollView() {
    return SingleChildScrollView(
      child: this,
    );
  }

  Widget addSmallCirclePoint({bool hidden = true}){
    return Stack(children: [
      Align(child: this,),
      Align(alignment: Alignment.topRight,child:
          Visibility(visible: hidden,child:
      Container(
        width: 5,
        height: 5,
        decoration: BoxDecoration(
          color: Colors.red,
          borderRadius: BorderRadius.circular(2.5)
        )),
      ),)
    ],);
  }

  Widget addReadNumber(){
    return Stack(children: [
      Align(child: this,),
      // Align(alignment: Alignment.topRight,child:
      // BlocBuilder<UnReadIndexCubit,int>(builder: (context,index){
      //   Future<int> future = MessageRepository.unReadMessage();
      //   return FutureBuilder<int>(future: future,builder: (context,data){
      //     Color color = Colors.transparent;
      //     Widget child = Container(
      //       width: 5,
      //       height: 5,
      //       decoration: BoxDecoration(
      //           color: color,
      //           borderRadius: BorderRadius.circular(2.5)
      //       ),
      //     );
      //     switch(data.connectionState){
      //       case ConnectionState.done:
      //         color = Colors.red;
      //         if(data.data == 0 || data.data == null){
      //           return child;
      //         }
      //         return Container(
      //           width: 12,
      //           height: 12,
      //           alignment: Alignment.center,
      //           decoration: BoxDecoration(
      //               color: color,
      //               borderRadius: BorderRadius.circular(6)
      //           ),
      //           child: Text("${data.data}",textAlign: TextAlign.center,style: TextStyle(color: Colors.white,fontSize: 10),),
      //         );
      //         break;
      //     }
      //     return child;
      //   },);
      // },)
      //   ,)
    ],);

  }
}
class TextColorModel{
  String text;
  bool isHighlight;
  TextColorModel({this.text,this.isHighlight});
}
extension TextEXT on Text {
  Widget addNumberColorText({Color color,double fontSize = 12,String regexp}){
    RegExp re = RegExp(r"\d+/");
    if(regexp != null){
      re = RegExp("$regexp");
    }
    try {
      List<RegExpMatch> items = re.allMatches(this.data).toList();
      List<TextSpan>children = [];
      int index = 0;
      for(int i = 0;i < items.length; i++){
        RegExpMatch item = items[i];
        int start = item.start;
        int end = item.end;
        String one = this.data.substring(index, start);
        String two = this.data.substring(start, end);
        children.add(TextSpan(text: one));
        if(two.contains("/")){
          two = two.split("/").first;
          children.add(TextSpan(
              text: two, style: TextStyle(color: AppColors.primaryColor,fontSize: fontSize)));
          children.add(TextSpan(
              text: "/", style: TextStyle(color: color??AppColors.textSecondaryColor,fontSize: fontSize)));
        }else {
          children.add(TextSpan(
              text: two, style: TextStyle(color: AppColors.primaryColor,fontSize: fontSize)));
        }
        index = end;
        if(i == items.length - 1){
          children.add(TextSpan(text: this.data.substring(end)));
        }
      }
      if(children.length == 0) return this;
      return RichText(text: TextSpan(children: children,style: TextStyle(color: color??AppColors.textSecondaryColor,fontSize: fontSize)),);
    }catch (e){
      print("报错");
    }
    return this;
  }
}